package com.shxt.framework.rbac.menu.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.shxt.framework.rbac.menu.dao.MenuDao;
import com.shxt.framework.rbac.menu.model.Menu;
import com.shxt.framework.rbac.menu.service.MenuService;
@Service
public class MenuServiceImpl implements MenuService {
	@Autowired
	private MenuDao menuDao;
	@Override
	public List<Menu> getChildMenuList( Integer role_id ) {
		return this.menuDao.getChildMenuList(role_id);
	}

	@Override
	public List<Menu> getParentMenuList( Integer role_id ) {
		return this.menuDao.getParentMenuList(role_id);
	}

	@Override
	public Map<String, Object> getTreeMap() {
		Map<String, Object> treeMap = new HashMap<String, Object>();
		treeMap.put("id", -1);
		treeMap.put("text", "菜单资源树");
		treeMap.put("icon", "fa fa-folder");

		Map<String, String> liAttrMap = new HashMap<>();
		liAttrMap.put("node_type", "root");
		treeMap.put("li_attr", liAttrMap);

		//1.获取父节点列表
		List<Menu> parentMenuList = this.menuDao.getParentNodeList();
		//重新组装
		List<Map<String, Object>> parentMapList = new ArrayList<>();
		for (Menu parent : parentMenuList) {
			//父节点
			Map<String, Object> parentMap = new HashMap<>();
			parentMap.put("id", parent.getMenu_id());
			parentMap.put("text", parent.getMenu_name());
			String icon = parent.getIcon().isEmpty()?"fa fa-folder":parent.getIcon();
			parentMap.put("icon", icon);

			liAttrMap = new HashMap<>();
			liAttrMap.put("node_type", "parent");
			parentMap.put("li_attr", liAttrMap);
			//获取对应的子节点列表
			List<Menu> childMenuList = this.menuDao.getChildNodeList(parent.getMenu_id());
			//重新组装
			List<Map<String, Object>> childMapList = new ArrayList<>();
			for(Menu child : childMenuList) {
				//子节点
				Map<String, Object> childMap = new HashMap<>();
				childMap.put("id", child.getMenu_id());
				childMap.put("text", child.getMenu_name());

				icon = child.getIcon().isEmpty()?"fa fa-file-picture-o":child.getIcon();
				childMap.put("icon", icon);

				liAttrMap = new HashMap<>();
				liAttrMap.put("node_type", "child");
				childMap.put("li_attr", liAttrMap);

				childMapList.add(childMap);//组装到childList当中
			}
			//存储子节点信息
			parentMap.put("children", childMapList);

			parentMapList.add(parentMap);//组装到新的容器当中
		}
		treeMap.put("children", parentMapList);//组装到新的容器当中

		return treeMap;
	}

	//	@Override
	//	public Menu load( Integer menu_id ) {
	//		return this.menuDao.load(menu_id);
	//	}

	@Override
	public List<Menu> getParentNodeList() {
		return this.menuDao.getParentNodeList();
	}
}
